import { EntitySchema } from "typeorm";
import { inject_entity_metadata } from "../constant";

export type EntityClassOrSchema = Function | EntitySchema;

export const InjectRepository = (entity: EntityClassOrSchema) => {
  return (target: any, key: any) => {
    let data = [];
    if (Reflect.hasOwnMetadata(inject_entity_metadata, target.constructor)) {
      data = Reflect.getMetadata(inject_entity_metadata, target.constructor);
    }
    data.push({ entityName: key, entity, name: target.constructor.name });
    Reflect.defineMetadata(inject_entity_metadata, data, target.constructor);
  };
};
